package com.sh.personnel.util;

import com.sh.personnel.vo.dto.MenuDto;
import com.sh.personnel.vo.dto.MetaDto;
import com.sh.personnel.vo.dto.SelectTreeDto;
import com.sh.personnel.vo.dto.MenuTreeDto;

import java.util.ArrayList;
import java.util.List;

/**
 * 生成树迭代工具类
 * @author 林思浩
 * @date 2020/03/13 15:15
 */
public class TreeUtils {

    /**
     * 外部调用生成生菜单树路由
     * @param menus
     * @return
     */
    public static List<MenuTreeDto> generateMenuTree(List<MenuTreeDto> menus) {
        List<MenuTreeDto> menuTree = new ArrayList<>();
        for(MenuTreeDto menu: menus) {
            if(menu.getParentId() == 0) {
                menu.setPath('/' + menu.getPath());
                menuTree.add(menu);
                recursiveMenu(menus, menu);
            }
        }
        return menuTree;
    }
    /**
     * 递归子菜单
     * @param menus
     * @param parentMenu
     */
    private static void recursiveMenu(List<MenuTreeDto> menus, MenuTreeDto parentMenu) {
        for(MenuTreeDto children: menus) {
            if(parentMenu.getId() == children.getParentId()) {
                parentMenu.getChildren().add(children);
                recursiveMenu(menus, children);
            }
        }
    }

    /**
     * 外部调用生成选择选择树
     * @param list
     * @return
     */
    public static List<SelectTreeDto> generateSelectTree(List<SelectTreeDto> list) {
        List<SelectTreeDto> deptTree = new ArrayList<>();
        for(SelectTreeDto dept: list) {
            if(dept.getParentId() == 0) {
                deptTree.add(dept);
                recursiveSelect(list, dept);
            }
        }
        return deptTree;
    }
    /**
     * 递归子选择
     * @param list
     * @param parentDept
     */
    private static void recursiveSelect(List<SelectTreeDto> list, SelectTreeDto parentDept) {
        for(SelectTreeDto children: list) {
            if(parentDept.getValue() == children.getParentId()) {
                parentDept.getChildren().add(children);
                recursiveSelect(list, children);
            }
        }
    }

    /**
     * 外部调用生成菜单树列表
     * @param menus
     * @return
     */
    public static List<MenuDto> generateMenuList(List<MenuDto> menus) {
        List<MenuDto> list = new ArrayList<>();
        for(MenuDto menu: menus){
            if(menu.getParentId() == 0) {
                list.add(menu);
                recursiveMenuList(menus, menu);
            }
        }
        return list;
    }

    /**
     * 递归子菜单列表
     * @param menus
     * @param parentMenu
     */
    private static void recursiveMenuList(List<MenuDto> menus, MenuDto parentMenu) {
        for(MenuDto children: menus) {
            if(parentMenu.getId() == children.getParentId()) {
                parentMenu.getChildren().add(children);
                recursiveMenuList(menus, children);
            }
        }
    }
}
